package tpi.gui.panels.info

import dominio.ubicaciones.Pais
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.panel.Panel
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XListView
import org.uqbar.wicket.xtend.XButton
import dominio.datos.Caracteristica
import org.apache.wicket.markup.html.form.TextField
import org.apache.wicket.model.Model

class CaracteristicasPanel extends Panel {
	
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	private final Pais villano
	
	new(String id, Pais pais) {
		super(id)
		this.villano = pais
		this.defaultModel = pais.asCompoundModel
		val Form<Pais> form = new Form<Pais>("caracteristicasForm", new CompoundPropertyModel(pais))
		val listView = new XListView("caracteristicas")
		listView.populateItem = [c |
			c.model = c.modelObject.asCompoundModel
			c.addChild(new Label("dato"))
			c.addChild(new XButton("eliminar").onClick = [| eliminar(c.modelObject)])
		]
		form.addChild(listView)
		val c = new TextField<String>("dato", new Model<String>)
		form.addChild(c)
		form.addChild(new XButton("agregarCaracteristica").onClick = [| 
			val dato = c.modelObject
			agregarCaracteristica(dato)
		])
		addChild(form)
	}
	
	def agregarCaracteristica(String dato) {
		val caracteristica = new Caracteristica(dato)
		caracteristica.validar()
		this.villano.caracteristicas += caracteristica
			
	}
	
	def eliminar(Caracteristica caracteristica) {
		this.villano.caracteristicas.remove(caracteristica)
	}
	
	
	
}